package com.bocai.kelalacar.ui.car.set.act;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.kelalacar.App;
import com.bocai.kelalacar.R;
import com.bocai.kelalacar.base.BaseActivity;
import com.bocai.kelalacar.bean.ApplyWayBean;
import com.bocai.kelalacar.ui.car.main.act.MainActivity;
import com.bocai.kelalacar.ui.car.set.contract.WithdrawTypeContract;
import com.bocai.kelalacar.ui.car.set.module.WithdrawTypeModule;
import com.bocai.kelalacar.ui.car.set.presenter.WithdrawTypePresenter;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by yuanfei on 2017/6/22.
 */

public class AddWithdrawTypeActivity extends BaseActivity implements WithdrawTypeContract.view {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.right_btn)
    Button rightBtn;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.ed_account)
    EditText edAccount;
    @BindView(R.id.btn_confirm)
    Button btnConfirm;
    private int index = 0;
    @BindView(R.id.ll_back)
    LinearLayout llBack;
    @Inject
    WithdrawTypePresenter presenter;
    private String account;

    @Override
    public int getLayoutId() {
        return R.layout.act_add_withdraw_type;
    }

    @Override
    public void initPresenter() {
        App.get(this).getAppComponent().addSub(new WithdrawTypeModule(this)).inject(this);
    }

    @Override
    public void initView() {
        initToolbar("提现方式");
        initBack(imgBack,llBack);
        index = getIntent().getIntExtra("index",0);
        account = getIntent().getStringExtra("account");
        if (index == 1){
            tvType.setText("支付宝账号");
            if (TextUtils.isEmpty(account)){
                edAccount.setHint("请输入支付宝账号");
            }else {
                edAccount.setText(account);
            }

        }else if (index == 2){
            tvType.setText("微信账号");
            if (TextUtils.isEmpty(account)){
                edAccount.setHint("请输入微信账号");
            }else {
                edAccount.setText(account);
            }
        }
        btnConfirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!TextUtils.isEmpty(edAccount.getText().toString())) {
                    presenter.addApplyWay("", index + "", "", edAccount.getText().toString());
                }
            }
        });
        edAccount.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (edAccount.getText().toString().length() > 0) {
                    btnConfirm.setEnabled(true);
                    btnConfirm.setTextColor(getResources().getColor(R.color.color_white));
                } else {
                    btnConfirm.setEnabled(false);
                    btnConfirm.setTextColor(getResources().getColor(R.color.text_color));
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    @Override
    public void applyWaySuccess(ApplyWayBean bean) {

    }

    @Override
    public void addApplyWaySuccess() {
        if (TextUtils.isEmpty(account)){
            showErrorTip("添加成功");
        }else{
            showErrorTip("修改成功");
        }
        setResult(19);
        finish();

    }

    @Override
    public void deleteApplyWaySuccess(int index) {

    }

    @Override
    public void walletApplySuccess() {

    }

    @Override
    public void showLoading(String title) {
        startProgressDialog(title);
    }

    @Override
    public void stopLoading() {
        stopProgressDialog();
    }

    @Override
    public void showErrorTip(String msg) {
        showShortToast(msg);
        if (msg.equals("您的登录已失效，请重新登录")){
            startActivity(new Intent(this,MainActivity.class).putExtra("index",1));
            finish();
        }
    }
}
